<?php

namespace App\Admin\Actions\Form;

use App\Admin\Actions\Imports\DeviceImport;
use Dcat\Admin\Widgets\Form;
use Maatwebsite\Excel\Facades\Excel;
use Dcat\Admin\Admin;
use App\Models\Project;
use App\Models\ProjectUser;

class DeviceForm extends Form
{
    public function handle(array $input)
    {
        try {
            //上传文件位置，这里默认是在storage中，如有修改请对应替换
            session()->put('proId', $input['projectId']);
            $file = public_path('/uploads/' . $input['file']);
            Excel::import(new DeviceImport(), $file);
            return $this->response()->success('导入成功')->refresh();
        } catch (\Exception $e) {
            return $this->response()->error($e->getMessage());
        }
    }

    public function form()
    {
        $list = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
        $res = Project::whereIn('projectId',$list)->pluck('projectname','projectId');
        $this->select('projectId','所在项目')->options($res)->required();
        $this->file('file', '上传设备（Excel）')->rules('required', ['required' => '文件不能为空'])->move('admin/upload/');
    }

}